const { salesmansModel } = require('./models/salesmansModel')

//新增销售员
module.exports.salesmansAdd = async params => {

  return await salesmansModel.create(params);


}
//获取销售员
module.exports.salesmansRend = async ({ val = '' }) => {
  const findSalesmanList = salesmansModel.find({
    isDel: false,//筛选出来没有被软删除的数据
    $or: [
      { name: { $regex: val, $options: '$i' } },
      { age: { $regex: val, $options: '$i' } },
      { sex: { $regex: val, $options: '$i' } },
      { tel: { $regex: val, $options: '$i' } },
    ]
  })
  return findSalesmanList;
}

//—————胡兴泽
//通过id查找销售员
module.exports.salesmansUpdata = async (params) => {
  // const _id = Object.keys(params)[0]
  // return await salesmansModel.find({_id});
  return await salesmansModel.find(params);
}

//销售员修改功能----通过_id修改学生的信息
module.exports.salesmansModifyById = async (params) => {
  if (params.Data) {
    const { _id, Data } = params;
    return await salesmansModel.updateOne({ _id }, Data);
  }else{
    return await salesmansModel.updateOne({_id:params._id},params);
  }
}

//通过_id软删除学生
module.exports.getSalesmansDeleteById = async params => {
  // 软删除
  return await salesmansModel.updateOne(params, { isDel: true });
}

//--------胡兴泽
module.exports.receiveSalesmanById = async params => {
  const result = await salesmansModel.find({ _id: params }).populate('customerId');
  return result;
}